
function initRequestFormDialog() {
    // form dialog
    $('#approval-request-form').dialog({
        autoOpen: false,
        closeOnEscape: true,
        modal: false,
        position: 'center',
        show: 'blind',
        hide: 'blind',
        width: '500px',
        open: function() {
            loadProfessors();
        },
        close: function() {
            $("#request_form").clearForm();
        }
    });
    $("#send_request").button().click(function(){
        $.ajax({
            type: 'post',
            url: $("#request_form").attr("action"),
            data: {requestForm: $("#request_form").serialize()},
            dataType: "html",
            success: function(data, textStatus, jqXHR) {
                $("#research_status").replaceWith(data);
                return false;
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert("error: " + textStatus + ", " + errorThrown);
                return false;
            }
        });
        $("#approval-request-form").dialog('close');
    });
}

function loadProfessors(){
    //check wheather already loaded
    var count = 0
    $("#prof_list > div").each(function(){
        count++;
    });

    if (count > 0) return;

    // load professor list
    $.ajax({
        type: 'get',
        url: '/approval/proflist',
        dataType: 'html',
        success: function(data, textStatus, jqXHR) {
            $("#prof_list").html(data);
            setHover();
            return false;
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert("error: " + textStatus + ", " + errorThrown);
            return false;
        }
    });
}

function setHover() {
    // row hover
    $("#prof_list > div").hover(
        function(){
            $(this).addClass('ui-state-hover');
            $(this).css('cursor', 'pointer')
        },
        function(){
            $(this).removeClass('ui-state-hover');
            $(this).css('cursor', 'default')
        }
    );

    // row click
    $("#prof_list > div").click(function(){
        var entid = this.id.replace('item','');
        $("#prof_id").val(entid);
        $("#prof_name").val($("#name" + entid).html());
    });
}

function setStatusHandler() {
    $("#ask").click(function(){
        $("#approval-request-form").dialog('open');
    });

    $("#review").click(function() {
        $.ajax({
            type: 'post',
            url: $("#review_form").attr("action"),
            //data: {requestForm: $("#review_form").serialize()},
            dataType: "html",
            success: function(data, textStatus, jqXHR) {
                $("#research_status").html(data);
                return false;
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert("error: " + textStatus + ", " + errorThrown);
                return false;
            }
        });
    });

    $("#publish").click(function() {
        $.ajax({
            type: 'post',
            url: $("#publish_form").attr("action"),
            dataType: "html",
            success: function(data, textStatus, jqXHR) {
                $("#research_status").html(data);
                return false;
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert("error: " + textStatus + ", " + errorThrown);
                return false;
            }
        });
    });

    $("#revision").click(function() {
        $.ajax({
            type: 'post',
            url: $("#revision_form").attr("action"),
            dataType: "html",
            success: function(data, textStatus, jqXHR) {
                $("#research_status").html(data);
                return false;
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert("error: " + textStatus + ", " + errorThrown);
                return false;
            }
        });
    });
}